/*
 * PopupLoginDialog.cpp
 *
 *  Created on: Jun 5, 2012
 *      Author: guillou
 */
#include <PopupOSAL.hpp>
#include <PopupLoginDialog.hpp>
#include <PopupSettings.hpp>
#include <PopupUtilities.hpp>

using namespace PopupUtils;
using namespace utilities;
using namespace Popup;

PopupLoginDialog::PopupLoginDialog(QWidget *parent)
  : QDialog(parent)
{
  setupUi(this);
  m_createAccountCheckbox->setChecked(false);
  m_confirmPasswordLabel->setVisible(false);
  m_confirmPasswordEdit->setVisible(false);
  m_networkSettings->setVisible(false);

  PopupSettings *_settings = PopupSettings::instance();

  m_loginEdit->setText(_settings->login());
  m_passwordEdit->setText(_settings->password());
  m_savePasswordCheckbox->setChecked(_settings->password().length() > 0);
  m_serverEdit->setText(_settings->serverName());
  m_portSpinbox->setValue(_settings->serverPort());
}

bool PopupLoginDialog::saveLoginParameters() {

  PopupSettings *_settings = PopupSettings::instance();
  int _serverport = _settings->serverPort();
  QString _servername = _settings->serverName();
  QString _login = _settings->login();

  _settings->setServerName(m_serverEdit->displayText());
  _settings->setServerPort(m_portSpinbox->value());
  _settings->setLogin(m_loginEdit->displayText());
  _settings->setSavePassword(m_savePasswordCheckbox->isChecked());
  if (m_savePasswordCheckbox->isChecked()) {
    _settings->setPassword(m_passwordEdit->text());
  }

  // If any of server/port/nickname has changed, we have to restart
  // client thread with the new parameters!
  if ((_settings->serverName() != _servername) ||
      (_settings->serverPort() != _serverport) ||
      (_settings->login()      != _login))
  {
    emit(signal_restartClient());
  }

  return _settings->serialize(PopupSettings::POPUP_SAVE_TO_FILE);
}

//==============================================================================
// Slots
//==============================================================================

void PopupLoginDialog::accept()
{
  if ((m_createAccountCheckbox->isChecked()) &&
      (m_passwordEdit->text().compare(m_confirmPasswordEdit->text()) != 0)) {
    utilities::showError("Passwords do not match!");
  }
  else {
    saveLoginParameters();
    emit signal_setConnectionParams(m_loginEdit->text(),
                                    m_passwordEdit->text(),
                                    m_serverEdit->text(),
                                    m_portSpinbox->value(),
                                    m_createAccountCheckbox->isChecked());
    QDialog::accept();
  }
}

void PopupLoginDialog::slot_badConnectionParams(ConnectionEcode p_ecode)
{
  switch (p_ecode) {
    case POPUP_CONNECTION_ERR_USER_ALREADY_CONNECTED:
      showError("Already connected! (Did somebody steal you account?)");
      break;
    case POPUP_CONNECTION_ERR_UNKNOWN_USER:
      showError("Fuck you bastard! You are not registered!");
      break;
    case POPUP_CONNECTION_ERR_INVALID_PASSWORD:
      error("Wrong password! You can do it bloody ass hole!");
      break;
    case POPUP_CONNECTION_ERR_EMPTY_LOGIN:
      show();
      break;
    case POPUP_CONNECTION_ERR_LOGIN_ALREADY_EXISTS:
      showError("Another user already has this login. Please choose another one.");
      break;
    case POPUP_CONNECTION_ERR_UNKNOWN:
    case POPUP_CONNECTION_ERR_SERVER_DOWN:
    case POPUP_CONNECTION_ERR_HANDSHAKE_FAILED:
    case POPUP_CONNECTION_ERR_SQLITE:
    default:
      break;
  }

  exec();
}
